<template>
  <el-drawer :title="title" :visible.sync="visible" append-to-body class="drawer-container" size="400px">
    <el-form ref="form" :model="formdata" :rules="formrule" label-position="right" label-suffix=":" label-width="100px">
      <el-form-item label="参数名称" prop="name">
        <el-input v-model="formdata.name" placeholder="请输入参数名称"/>
      </el-form-item>
      <el-form-item label="参数键名" prop="code">
        <el-input v-model="formdata.code" placeholder="请输入参数键名"/>
      </el-form-item>
      <el-form-item label="参数键值" prop="value">
        <el-input v-model="formdata.value" placeholder="请输入参数键值"/>
      </el-form-item>
      <el-form-item label="系统内置" prop="built">
        <el-switch v-model="formdata.built" active-color="green" active-text="是" inactive-color="red"
                   inactive-text="否"/>
      </el-form-item>
      <el-form-item label="参数状态" prop="status">
        <el-radio-group v-model="formdata.status">
          <el-radio v-for="item in localDict.system_status" :key="item.value" :label="item.value">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input-number v-model="formdata.sort" placeholder="请输入参数序号"/>
      </el-form-item>
      <el-form-item label="参数描述">
        <el-input v-model="formdata.remark" :rows="3" placeholder="请输入参数描述" type="textarea"/>
      </el-form-item>
    </el-form>
    <div class="drawer-footer">
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-drawer>
</template>

<script>

import {getInfo, save} from "@/api/base";
import {getStaticData} from "@/api/common";

export default {
  name: "ParamModel",
  components: {},
  data() {
    return {
      title: '',
      visible: false,
      baseUrl: '/config/param/',
      formdata: {},
      formrule: {
        // 表单校验
        code: [
          {required: true, message: "参数标识不能为空", trigger: "blur"},
          {pattern: /^\w{2,30}$/, message: '参数标识只能包含数字、字母、下划线,长度在2-30个字符', trigger: 'blur'},
        ],
        name: [
          {required: true, message: "参数名称不能为空", trigger: "blur"}
        ],
        value: [
          {required: true, message: "参数值不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "参数状态不能为空", trigger: "blur"}
        ],
      },
      localDict: {
        system_status: [],
      },
    }
  },
  created() {
    this.getLocalDict();
  },
  methods: {
    add() {
      this.resetForm('form')
      this.formdata = {
        status: '00'
      }
      this.visible = true
      this.title = '新增参数'
    },
    edit(id) {
      this.resetForm('form')
      getInfo(this.baseUrl, id).then(res => {
        this.formdata = res.data
        this.visible = true
        this.title = '修改参数'
      })
    },
    async getLocalDict() {
      await getStaticData('system-status').then(res => {
        this.localDict.system_status = res.data
      })
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          save(this.baseUrl, this.formdata).then(() => {
            this.$modal.msgSuccess("保存成功");
            this.visible = false;
            this.resetForm('form')
            this.$emit('ok')
          });

        }
      });
    },
    handleCancel() {
      this.visible = false
      this.resetForm('form')
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
